Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

292
Visualizações
Twig "trans with" not working

I'm using the twig trans tag in my templates, and I want to pass variables in it like so:

{% trans with {
   '%link_start%': '<a href="http://www.google.nl/">',
   '%link_end%': '</a>'
} %}
    This %link_start%disclaimer%link_end% applies to all of our messages.
{% endtrans %}

But this gives me the following exception which points to the twig template at the {% trans with line:

PHP Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'Unexpected token "punctuation" of value "{" ("end of statement block" expected).' in [twig-template:line]

Even when I copy & paste the examples from the Symfony documentation I get the same exception. So I'm at a loss, what am I doing wrong here?

FYI: I'm using Twig 1.33 with the i18n extension enabled (and I'm not using the Symfony framework)

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Twig doesn't support trans with out of the box. It is part of the Symfony translation extension. That explains why even the official Symfony documentation doesn't work - you are not using Symfony.

See this issue: https://github.com/twigphp/Twig-extensions/issues/74. There is a pull request to support trans with, but it hasn't been merged.

You might want to use the Symfony Translation Component in your application. You can use Symfony Components in your application, even without using the full Symfony (Framework) stack.

I haven't tried it, but you can try to use jhogervorst/Twi18n instead.

about 4 years ago · Santiago Trujillo Relatório

0

As a workaround you can use the filter tag with replace.

{% filter replace({'%foo%': 'blue', '%bar%': 'red'}) %}
  {% trans %}
    I like %foo% and %bar% messages.
  {% endtrans %}
{% endfilter %}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda